// pages/school/add/add.js
var schoolservice = require("../../../apiservice/school/schoolservice");
var useridentity = wx.getStorageSync('useridentity');
const jsonData = require("../jsondata.js");
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		street: {
			new_streetid: '',
			new_name: '请选择',
			province: null,
			city: null,
			area: null
		},
		ownerName: useridentity.fullname,
		schoolItem: {
			new_name: null,
			new_stage: 1,
			new_property: 1,
			new_level: 1,
			new_street: null,
			new_address: null,
			new_contact: null,
			new_contactName: null,
			new_mobile: null,
			userid: useridentity.userid
		},
		/**
		 * 选项集
		 */
		stageArray: jsonData.stageArray,
		stageIndex: 0,
		levelArray: jsonData.levelArray,
		levelIndex: 0,
		propertyArray: jsonData.propertyArray,
		propertyIndex: 0
	},
	/**
	 * 添加学校信息
	 */
	onSave: function() {
		if (this.checkInfo() == false) {
			return false;
		}
		var province = this.data.schoolItem.provinceName
		var city = this.data.schoolItem.cityName
		var area = this.data.schoolItem.areaName
		schoolservice.saveSchoolInfo(this.data.schoolItem).then(function(success) {
			console.log(success);
			wx.showToast({
				title: '添加成功',
				icon: 'success',
				duration: 2000
			});
			let pages = getCurrentPages();
			let prevPage = pages[pages.length - 2];

			if (province && city && area &&
				prevPage.data.hasOwnProperty("region")) {
				prevPage.setData({
					region: [province, city, area]
				}, function() {
					wx.navigateBack({
						delta: 1
					});
				})
			} else {
				wx.navigateBack({
					delta: 1
				});
			}


			// wx.redirectTo({
			//   url:'../list/list?type=detail&province='+province +"&city="
			//   + city+"&area=" +area
			// });

		}).catch(function(ex) {
			wx.showToast({
				title: '添加失败',
				icon: 'none',
				duration: 2000
			});
			console.log(ex);
		})
	},
	/***
	 * 选择框赋值
	 */
	bindStageChange: function(e) {
		var stageId = this.data.stageArray[e.detail.value].id;
		//console.log(stageId);
		this.setData({
			stageIndex: e.detail.value,
			['schoolItem.new_stage']: stageId
		});
		//console.log(this.data.schoolItem);
	},
	bindLevelChange: function(e) {
		var levelId = this.data.levelArray[e.detail.value].id;
		this.setData({
			levelIndex: e.detail.value,
			['schoolItem.new_level']: levelId
		});
	},
	bindPropertyChange: function(e) {
		var propertyId = this.data.propertyArray[e.detail.value].id;
		this.setData({
			propertyIndex: e.detail.value,
			['schoolItem.new_property']: propertyId
		});
	},
	/***
	 * 文本赋值
	 * 
	 */
	bindNameChange: function(e) {
		//console.log(e);
		this.setData({
			['schoolItem.new_name']: e.detail
		});
	},
	bindAddressChange: function(e) {
		this.setData({
			['schoolItem.new_address']: e.detail
		});
	},
	bindContactNameChange: function(e) {
		this.setData({
			['schoolItem.new_contactName']: e.detail
		});
	},
	bindMobileChange: function(e) {
		this.setData({
			['schoolItem.new_mobile']: e.detail
		});
		// console.log(this.data.schoolItem);
	},
	/**
	 * 跳转到街道列表
	 * 
	 */
	toStreetListPage: function() {
		//debugger;
		let province = this.data.schoolItem.provinceName;
		let city = this.data.schoolItem.cityName;
		let area = this.data.schoolItem.areaName;
		let toUrl = '../../street/list/list?type=choose'
		if (province && city && area) {
			toUrl += `&province=${province}&city=${city}&area=${area}`;
		}

		console.log("跳转到街道详细列表");
		wx.navigateTo({
			url: toUrl
		});

	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		//console.log(this.data.street);
		debugger;
		this.setData({
			['schoolItem.provinceName']: options.province,
			['schoolItem.cityName']: options.city,
			['schoolItem.areaName']: options.area
		})
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {
		// console.log(this.data.street);
		var streetid = this.data.street.new_streetid;
		if (streetid) {
			this.setData({
				['schoolItem.new_street']: streetid,
				['schoolItem.provinceName']: this.data.street.province,
				['schoolItem.cityName']: this.data.street.city,
				['schoolItem.areaName']: this.data.street.area
			});
		}
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	},
	checkInfo: function() {
		// console.log("checkinfo:");
		// console.log(this.data.schoolItem.new_name);
		// console.log(this.data.schoolItem.new_street);
		if (this.data.schoolItem.new_name == null || this.data.schoolItem.new_name == "") {
			wx.showToast({
				title: '学校名称为空',
				icon: 'none',
				duration: 2000
			})
			return false;
		}
		if (this.data.schoolItem.new_street == null || this.data.schoolItem.new_street == "") {
			wx.showToast({
				title: '请选择街道',
				icon: 'none',
				duration: 2000
			})
			return false;
		}
		return true;
	}
})
